package com.koc.robot;

public class IntroState extends BasicState
{
	public IntroState()
	{
		mStateTag = State.STATE_INTRO;
	}
	
	@Override
	public void load()
	{
		logo = new Texture(Global.context);
		logo.load("stabyourself.png");
		logoblood = new Texture(Global.context);
		logoblood.load("stabyourselfblood.png");
		
		Global.soundplayer.preloadEffect("sounds/stab.ogg");
	}
	
	@Override
	public void unload()
	{
		Global.soundplayer.unloadEffect("sounds/stab.ogg");
	}
	
	@Override
	public void update(float dt)
	{
		if (introprogress < introduration + blackafterintro)
		{ 
			introprogress += dt;
			if (introprogress > introduration + blackafterintro)
			{
				introprogress = introduration + blackafterintro;
			}
			
			// I don't know we on device, the sound plays earlier than on PC.
			// may be it is because preload logic, I change 0.5 to 2.0 here
			if (introprogress > 2.0f && !playedwilhelm)
			{
				Global.soundplayer.playEffect("sounds/stab.ogg", false);
				playedwilhelm = true;
			}
			
			if (introprogress == introduration + blackafterintro)
			{
				Scheduler.instance().tostate(State.STATE_MENU);
			}
		}
	}
	
	@Override
	public void render()
	{
		if (introprogress >= 0 && introprogress < introduration)
		{ 
			Color color = new Color(1.0f, 1.0f, 1.0f);
			color.A = 1.0f;
			if (introprogress < introfadetime)
			{
				color.A = introprogress / introfadetime;
			}
			else if (introprogress >= introduration - introfadetime)
			{
				color.A = (1 - (introprogress - (introduration - introfadetime)) / introfadetime);
			}
			
			GraphicsHelper.setColor(color); 
			
			if (introprogress > introfadetime + 0.3 && introprogress < introduration - introfadetime)
			{
				float y = (float)(introprogress - 0.2 - introfadetime) / (introduration - 2 * introfadetime) * 206 * 5;
				GraphicsHelper.draw(logo, Global.screenwidth / 2 - 142.0f, Global.screenheight / 2 - 150.0f);
				GraphicsHelper.setScissor(0, (int)(Global.screenheight / 2 + 150 - y), Global.screenwidth, (int)y);
				GraphicsHelper.draw(logoblood, Global.screenwidth / 2 - 142, Global.screenheight / 2 - 150);
				GraphicsHelper.setScissor();
			}
			else if (introprogress >= introduration - introfadetime)
				GraphicsHelper.draw(logoblood, Global.screenwidth / 2 - 142, Global.screenheight / 2 - 150);
			else
				GraphicsHelper.draw(logo, Global.screenwidth / 2 - 142, Global.screenheight / 2 - 150);
		} 
		GraphicsHelper.setColor(Global.fillcolor); 
		GraphicsHelper.draw(Global.scanlineimg, 0, (Global.creditss * 3.0f) % 5 - 5);
	}
	
	private float introduration = 2.5f; 
	private float blackafterintro = 0.3f;
	private float introfadetime = 0.5f;
	private float introprogress = -0.2f;
	
	private boolean playedwilhelm = false;
	
	private Texture logo;
	private Texture logoblood;
	
}
